package com.suray.wcs.service.test.action.checkmap.rgv;

import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.wcs.res.enums.RgvStatus;
import com.suray.wcs.res.enums.RgvType;
import com.suray.wcs.service.pojo.Rgv;
import com.suray.wcs.service.system.entity.RgvDB;
import com.suray.wcs.service.test.base.BaseThread;
import com.suray.wcs.service.util.WcsUtil;

import java.util.ArrayList;
import java.util.List;

public class RgvCheckScanGroundCode extends BaseThread {

    private Rgv rgv;
    private RgvScanGroundContainer container;

    public RgvCheckScanGroundCode(Rgv rgv) {
        super(rgv.getRgvName() + "地码检测", 500);

        this.rgv = rgv;
        this.container = new RgvScanGroundContainer(rgv);
    }

    @Override
    protected void process() {
        RgvStatus rgvStatus = rgv.getRgvStatus();
        if (rgvStatus == RgvStatus.OFF_LINE) {
            return;
        }

        String groundCode = rgv.getFullGroundCode();

        if (groundCode == null || rgv.getLocationCode() == null || rgv.getLocationCode().equals("255")) {
            return;
        }

        LoggerUtil.info("RgvScanGroundCode-->>扫到地码:" + groundCode, this.getClass());

        checkGroundCode(groundCode);
    }

    private void checkGroundCode(String groundCode) {
        container.analyzeGroundCodes(groundCode);
    }

    public static void test() {
        RgvDB rgvDB = new RgvDB();
        rgvDB.setRgvNo(0);
        rgvDB.setRgvType(RgvType.FOUR_GENERATIONS.getValue());
        Rgv rgv = new Rgv(rgvDB);
        rgv.setRgvStatus(RgvStatus.RUNING);
        rgv.setDirection(1);
        rgv.setGroundCode("111201");
        rgv.setLocationCode("490");
        RgvCheckScanGroundCode rgvCheckScanGroundCode = new RgvCheckScanGroundCode(rgv);
        rgvCheckScanGroundCode.start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                List<RgvRunInfo> rgvRunInfos = generalCodeList();
                while (true) {
                    try {
                        if (rgvRunInfos.size() == 0) {
                            break;
                        }
                        process(rgv, rgvCheckScanGroundCode, rgvRunInfos);
                    } catch (Exception e) {
                        WcsUtil.exceptionLog(e, this.getClass());
                    }

                    WcsUtil.delay(700);
                }

                LoggerUtil.info("RgvScanGroundCode-->>地码测试结束", this.getClass());
            }
        }).start();
    }

    private static void process(Rgv rgv, RgvCheckScanGroundCode rgvCheckScanGroundCode, List<RgvRunInfo> rgvRunInfos) {
        RgvRunInfo runInfo = rgvRunInfos.remove(0);
        rgv.setDirection(runInfo.direction);
        rgv.setGroundCode(runInfo.groundCode);
        rgv.setLocationCode(runInfo.locationCode);
    }

    private static List<RgvRunInfo> generalCodeList() {
        List<RgvRunInfo> rgvRunInfos = new ArrayList<>();
        rgvRunInfos.add(new RgvRunInfo("111201", "490", 1));
        rgvRunInfos.add(new RgvRunInfo("111201", "490", 1));
        rgvRunInfos.add(new RgvRunInfo("111201", "500", 1));
        rgvRunInfos.add(new RgvRunInfo("111201", "510", 1));
        rgvRunInfos.add(new RgvRunInfo("111114", "490", 1));
        rgvRunInfos.add(new RgvRunInfo("111114", "500", 1));
        rgvRunInfos.add(new RgvRunInfo("111114", "510", 1));
        rgvRunInfos.add(new RgvRunInfo("111113", "510", 1));
        rgvRunInfos.add(new RgvRunInfo("111112", "510", 1));
        rgvRunInfos.add(new RgvRunInfo("040600", "510", 1));
        rgvRunInfos.add(new RgvRunInfo("040500", "510", 1));
        rgvRunInfos.add(new RgvRunInfo("111117", "490", 1));//
        rgvRunInfos.add(new RgvRunInfo("111117", "490", 1));//

        rgvRunInfos.add(new RgvRunInfo("041100", "490", 1));
        rgvRunInfos.add(new RgvRunInfo("041100", "500", 1));
        rgvRunInfos.add(new RgvRunInfo("041100", "500", 1));
        rgvRunInfos.add(new RgvRunInfo("041100", "500", 2));
        rgvRunInfos.add(new RgvRunInfo("041100", "500", 2));
        rgvRunInfos.add(new RgvRunInfo("041100", "510", 2));
        rgvRunInfos.add(new RgvRunInfo("040700", "490", 2));
        rgvRunInfos.add(new RgvRunInfo("040700", "500", 2));
        rgvRunInfos.add(new RgvRunInfo("040700", "500", 2));
        rgvRunInfos.add(new RgvRunInfo("040700", "500", 3));
        rgvRunInfos.add(new RgvRunInfo("040700", "500", 3));
        rgvRunInfos.add(new RgvRunInfo("040700", "490", 3));

        rgvRunInfos.add(new RgvRunInfo("111115", "510", 3));
        rgvRunInfos.add(new RgvRunInfo("111115", "490", 3));
        rgvRunInfos.add(new RgvRunInfo("111116", "510", 3));
        rgvRunInfos.add(new RgvRunInfo("111116", "500", 3));
        rgvRunInfos.add(new RgvRunInfo("111116", "500", 3));
        rgvRunInfos.add(new RgvRunInfo("111116", "500", 3));
        rgvRunInfos.add(new RgvRunInfo("111116", "500", 3));
        rgvRunInfos.add(new RgvRunInfo("111116", "500", 3));

        return rgvRunInfos;
    }

    static class RgvRunInfo{
        String groundCode;
        String locationCode;
        Integer direction;

        RgvRunInfo(String groundCode, String locationCode, Integer direction) {
            this.groundCode = groundCode;
            this.locationCode = locationCode;
            this.direction = direction;
        }
    }
}
